package edu.fmi.st.epf.types.skill.common.util;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.epf.library.edit.util.ModelStructure;
import org.eclipse.epf.uma.CustomCategory;
import org.eclipse.epf.uma.Section;
import org.eclipse.epf.uma.UmaFactory;

import edu.fmi.st.epf.types.skill.common.handler.SkillCustomTypeHandler;
import eu.tanov.epf.pv.service.types.util.CustomTypeHelper;

public class SkillHelper {
	public static final String CATEGORY_NAME = "Skills"; //$NON-NLS-1$

	public static final String MAKE_SKILL_TEXT = "Skill";
	public static final String MAKE_GROUP_TEXT = "Skill Group";

	public static final String[] SKILLS_PATH;
	static {
		SKILLS_PATH = new String[ModelStructure.DEFAULT_DOMAIN_PATH.length];
		// -1, because last is used for Project Practice category
		System.arraycopy(ModelStructure.DEFAULT_DOMAIN_PATH, 0, SKILLS_PATH, 0, SKILLS_PATH.length - 1);
		SKILLS_PATH[SKILLS_PATH.length - 1] = SkillHelper.CATEGORY_NAME;
	}

	/**
	 * helper
	 */
	private SkillHelper() {
	}

	public static boolean isSkill(Object o) {
		if (!(o instanceof CustomCategory)) {
			return false;
		}
		final CustomCategory customCategory = (CustomCategory) o;

		for (Section section : customCategory.getPresentation().getSections()) {
			if (MAKE_SKILL_TEXT.equals(section.getName())) {
				return true;
			}
		}
		return false;
	}

	public static boolean isSkillGroup(Object o) {
		if (!(o instanceof CustomCategory)) {
			return false;
		}
		final CustomCategory customCategory = (CustomCategory) o;

		for (Section section : customCategory.getPresentation().getSections()) {
			if (MAKE_GROUP_TEXT.equals(section.getName())) {
				return true;
			}
		}
		return false;
	}

	public static CustomCategory makeCategory(String name) {
		CustomCategory result = UmaFactory.eINSTANCE.createCustomCategory();
		Section section = UmaFactory.eINSTANCE.createSection();
		section.setName(name);
		result.getPresentation().getSections().add(section);
		return result;
	}

	public static EClass getCustomType() {
		return CustomTypeHelper.getCustomType(SkillCustomTypeHandler.TYPE_NAME);
	}

	public static boolean isRegistered() {
		return CustomTypeHelper.isRegistered(SkillCustomTypeHandler.TYPE_NAME);
	}

}
